<template>
    <view class="goods-item">
        <gcheck v-model="goods.select" :id="goods.id" @change="toggleSel" />
        <view class="w-r">
            <view class="goods-img">
                <image class="gimg" :src="$imgUrl + goods.thumb"
                    @click.stop="$go(`/pages/goods/index?id=${goods.id}`)" />
            </view>
            <view class="wrap-content">
                <view class="wrap-info">
                    <view class="info-top">
                        <view class="name">{{ goods.name }}</view>
                        <view class="sel">
                            {{ goods.mdl }}
                        </view>
                    </view>
                    <view class="price">
                        <view class="pbox">
                            <text class="money mprice"> <text class="pi">￥</text> {{ goods.activity_price }}</text>
                            <text class="price-old" v-if="goods.price">￥{{ goods.price }}</text>
                        </view>
                        <view class="btns">
                            <view class="btn" :class="goods.count == 1 ? 'disabled' : ''" @click.stop="transCount(2)">-
                            </view>
                            <view class="num">{{ goods.count }}</view>
                            <view class="btn" @click.stop="transCount(1)">+ </view>
                        </view>
                    </view>
                </view>

            </view>
        </view>
    </view>
</template>

<script setup>
import gcheck from './gcheck.vue'
const { proxy } = getCurrentInstance()
const emit = defineEmits(['fresh'])
const props = defineProps({
    goods: {
        type: Object,
        default: {}
    }
})

async function transCount(type) {
    if (type === 2) {
        if (props.goods.count == 1) return
        props.goods.count--
    }
    if (type === 1) {
        props.goods.count++
    }
    let res = await proxy.$api('CartOp', {
        gids: props.goods.id,
        type
    }, false)
    if (res.code == 0) {
        emit('fresh')
    }
}

function toggleSel() {
    emit('fresh')
}

</script>

<style lang="scss" scoped>
.goods-item {
    width: 100%;
    display: flex;
    border-top: 1px solid #f5f5f5;
    box-sizing: border-box;
    padding: 24rpx 0;

    .w-r {
        flex: 1;
        width: 100%;
        display: flex;
    }



    .goods-img {
        width: 180rpx;
        height: 180rpx;
        border-radius: 8rpx;
        margin-right: 16rpx;
        position: relative;

        .gimg {
            width: 100%;
            height: 100%;
            border-radius: 8rpx;
        }
    }

    .wrap-content {
        flex: 1;
        height: auto;
        padding-right: 24rpx;
    }

    .wrap-info {
        width: 100%;
        flex-grow: 1;
        font-size: 24rpx;
        display: flex;
        flex-flow: column;

        .info-top {
            width: 100%;
            display: flex;
            flex-flow: column;
        }

        .name {
            flex-shrink: 1;
            height: auto;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            overflow: hidden;
            text-overflow: ellipsis;
            -webkit-box-orient: vertical;
            color: #000;
            margin-bottom: 16rpx;
            font-size: 24rpx;
        }


        .sel {
            flex-shrink: 1;
            width: 100%;
            display: flex;
            align-items: center;
            padding: 8rpx 0;
            box-sizing: border-box;
            color: #999999;
            font-size: 22rpx;
            margin-bottom: 16rpx;
        }

        .price {
            align-items: center;
            flex-grow: 1;
            display: flex;
            justify-content: space-between;
            margin-bottom: 10rpx;

            .btns {
                display: flex;
                align-items: center;
                border-radius: 4rpx;
                border: 1rpx solid #f0f1f3;

                .btn {
                    width: 60rpx;
                    line-height: 60rpx;
                    text-align: center;
                    box-sizing: border-box;
                }

                .disabled {
                    opacity: 0.2;
                }
            }

            .num {
                width: 80rpx;
                text-align: center;
                line-height: 60rpx;
                box-sizing: border-box;
                color: #1f1f1f;
            }

            .pbox {
                display: flex;
                align-items: center
            }

            .money {
                font-size: 30rpx;
                font-weight: bold;

                .pi {
                    position: relative;
                    left: 4rpx;
                    font-size: 24rpx;
                }
            }

            .n-price {
                &::after {
                    // content: "单件折后";
                    display: inline;
                    margin-left: 6rpx;
                    font-size: 18rpx;
                }
            }

            .price-old {
                margin-left: 10rpx;
                color: #4f4f4f;
                text-decoration: line-through;
                font-size: 18rpx;
                font-weight: normal;
                position: relative;
                top: 2rpx;

                &::before {
                    // content: "免税价:";
                    display: inline-block;
                    text-decoration: none;
                }
            }
        }
    }

}
</style>